// pages/registered/registered.ts
import { trimStr } from "../../utils/util";
import { setUser, getUserInfo } from "../../utils/api";
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
let clickNextFlag = true;
let index = 0;
Page({
  data: {
    steps: [
      {
        desc: '账号密码',
      },
      {
        desc: '信息完善',
      },
      {
        desc: '注册结果',
      }
    ],
    activeIndex: 0,
    current: 0,
    userName: "",
    passWord: "",
    confirmPassWord: "",
    phone: "",
    phoneErrorMessage: "",
    useErrorMessage: "",
    pwdErrorMessage: "",
    cpwdErrorMessage: "",
    addressName: "",
    latitude: 0,
    longitude: 0
  },
  // 返回首页
  onOnePage() {
    this.setData({
      activeIndex: 0,
      current: 0
    })
  },
  // 返回上一页
  onPrePage() {
    this.setData({
      current: this.data.current - 1,
      activeIndex: this.data.activeIndex - 1
    })
  },
  // 下一步按钮2
  onNextPageT(e: any) {
    if (!this.data.phone) {
      Toast.fail('手机号错误');
      return
    }
    if (!this.data.latitude) {
      Toast.fail('纬度错误');
      return
    }
    if (!this.data.longitude) {
      Toast.fail('精度错误');
      return
    }
    this.setData({
      current: 2,
      activeIndex: 2
    })
    this.onRegisteredFn();
  },
  // 辅助定位
  onPositon() {
    const that = this;
    wx.chooseLocation({
      success(e) {
        that.setData({
          addressName: e.address,
          latitude: e.latitude,
          longitude: e.longitude
        })
      },
      fail(e) {
        console.log(e);
      }
    })
  },
  // 下一步按钮
  onNextPage(e: any) {
    if (!this.data.userName) {
      Toast.fail('用户名错误');
      return
    }
    if (!this.data.passWord) {
      Toast.fail('密码错误');
      return
    }
    if (!this.data.confirmPassWord) {
      Toast.fail('密码不一致');
      return
    }
    this.setData({
      current: 1,
      activeIndex: 1
    })
  },
  // 轮播图切换触发
  onSwiperChange(e: any) {
    if (clickNextFlag) {
      index = e.detail.current;
      clickNextFlag = false
    } else {
      Toast.fail('任务未完成');
      index = this.data.current
    }
    this.setData({
      current: index,
      activeIndex: index
    })
  },
  // 返回上一级
  onBackLogin() {
    wx.navigateTo({
      url: "../login/login"
    })
  },
  // 校验用户的手机号码
  synchronousPhoneData(msg: any) {
    const exp = /^1(3[0-9]|5[0-3,5-9]|7[1-3,5-8]|8[0-9])\d{8}$/;
    if (!msg.detail) {
      this.setData({
        phoneErrorMessage: "账号不允许为空",
        phone: ""
      })
    }
    if (!exp.test(msg.detail)) {
      this.setData({
        phoneErrorMessage: "手机号格式不正确",
        phone: ""
      })
    }
    if (exp.test(msg.detail)) {
      this.setData({
        phoneErrorMessage: "",
        phone: msg.detail
      })
    }
  },
  // 校验用户名的正确性
  synchronousSetUserNameData(msg: any) {
    msg.detail = trimStr(msg.detail);
    const exs = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
    if (!msg.detail) {
      this.setData({
        useErrorMessage: "账号不允许为空",
        userName: ""
      })
    }
    if (!exs.test(msg.detail)) {
      this.setData({
        useErrorMessage: "账号不符合邮箱规则请检查",
        userName: ""
      })
    } else {
      this.setData({
        useErrorMessage: "",
        userName: trimStr(msg.detail)
      })
    }
  },
  // 校验密码正确性
  synchronousSetPassWordData(msg: any) {
    var reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/;
    if (!msg.detail) {
      this.setData({
        passWord: "",
        pwdErrorMessage: "密码不允许为空"
      })
    }
    if (!reg.test(msg.detail)) {
      this.setData({
        passWord: "",
        pwdErrorMessage: "密码为6-16位字母和数字组合 "
      })
    } else {
      this.setData({
        pwdErrorMessage: "",
        passWord: trimStr(msg.detail)
      })
    }
  },
  // 校验二级密码正确性
  synchronousSetConfirmPassWordData(msg: any) {
    var reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/;
    if (!msg.detail) {
      this.setData({
        confirmPassWord: "",
        cpwdErrorMessage: "密码不允许为空"
      })
      return;
    }
    if (!reg.test(msg.detail)) {
      this.setData({
        confirmPassWord: "",
        cpwdErrorMessage: "密码为6-16位字母和数字组合 "
      })
      return;
    }
    if (this.data.passWord !== msg.detail) {
      this.setData({
        confirmPassWord: "",
        cpwdErrorMessage: "两次密码输入不一致"
      })
      return;
    }
    console.log(msg.detail, this.data.passWord);
    this.setData({
      cpwdErrorMessage: "",
      confirmPassWord: trimStr(msg.detail)
    })
  },
  // 注册
  onRegisteredFn() {
    if (!this.data.userName) {
      Toast.fail('用户名错误');
      return
    }
    if (!this.data.passWord) {
      Toast.fail('密码错误');
      return
    }
    if (!this.data.confirmPassWord) {
      Toast.fail('密码不一致');
      return
    }
    if (!this.data.phone) {
      Toast.fail('手机号错误');
      return
    }
    if (!this.data.latitude) {
      Toast.fail('纬度错误');
      return
    }
    if (!this.data.longitude) {
      Toast.fail('精度错误');
      return
    }
    setUser({
      userName: this.data.userName,
      passWord: this.data.passWord,
      phone: this.data.phone,
      placeNames: this.data.addressName,
      latitude: this.data.latitude,
      longitude: this.data.longitude
    }).then(res => {
      if (res.flag) {
        getUserInfo({
          userName: this.data.userName,
          passWord: this.data.passWord
        }).then(res => {
          if (res.flag) {
            wx.setStorage({ key: "userInfo", data: res.data });
            wx.switchTab({
              url: "../home/home"
            })
          }
        })
      }
    })
  }
})